package ar.edu.uba.fi.jmit.graphvisualizer.client.widget;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.googlecode.gchart.client.GChart;

public abstract class GraphVisualizerChart extends GChart {
	
	protected String monitorName;
	protected String applicationName;
	protected String labelX;
	protected String labelY;
	protected final static int MAX_TICK_COUNT = 8;
	private Set<Integer> differentValues = new HashSet<Integer>();

	/**
	 * Se agrega un nuevo valor al monitor. Recordar que algunos monitores
	 * no agregan valores de a uno sino que pueden tener mas de una linea
	 * @param messageInfo
	 */
	protected abstract void addValue(Map<String, Serializable> messageInfo);
	
	/**
	 * Configura este monitor
	 * @param configuration
	 */
	public abstract void configure(Map<String, Serializable> configuration);
	
	/**
	 * Retorna el tipo de Monitor como llega desde el MonitorEngine 
	 */
	public abstract String getMonitorType();
	
	public String getApplicationName() {
		return applicationName;
	}
	
	public String getMonitorName() {
		return monitorName;
	}
	
	protected void configureView() {
		setChartTitle("<b>" + monitorName  + "</b>");
		setChartSize(500, 150);
		getYAxis().setAxisLabel(labelY);
		getYAxis().setTickLabelFormat("####");
	    getYAxis().setTickCount(1);
	    getYAxis().setAxisMin(0);
	    
	    getXAxis().setAxisLabel(labelX);
		getXAxis().setHasGridlines(true);
		getXAxis().setAxisVisible(true);
		getXAxis().setTickLabelFormat("=(Date) hh:mm:ss");
		getXAxis().setTickLocation(TickLocation.OUTSIDE);
	    getXAxis().setTickCount(5);
	}
	
	protected void configueCurve(int curve, String color) {
		getCurve(curve).getSymbol().setFillSpacing(0);
//		getCurve(curve).getSymbol().setFillThickness(3);
		getCurve(curve).getSymbol().setBorderWidth(1);
		getCurve(curve).getSymbol().setBorderColor(color);
		getCurve(curve).getSymbol().setBackgroundColor(color);
		getCurve(curve).getSymbol().setSymbolType(SymbolType.LINE);
		getCurve(curve).getSymbol().setHeight(0);
		getCurve(curve).getSymbol().setWidth(0);

		//		getCurve(curve).getSymbol().setWidth(0);
	}
	
	protected void refreshYAxisTickCount(int newValue) {
		differentValues.add(newValue);
		if(getYAxis().getTickCount() == MAX_TICK_COUNT || getYAxis().getTickCount() > differentValues.size()){
			return;
		}
		if(differentValues.size()>= MAX_TICK_COUNT) {
			getYAxis().setTickCount(MAX_TICK_COUNT);
		}else {
			getYAxis().setTickCount(differentValues.size());
		}
	}
	
	protected void refreshYAxisTickCount(Long newValue) {
		this.refreshYAxisTickCount(newValue.intValue());
	}
}
